<?php

namespace service\admin;

use service\BaseService;

class EmployeePerformanceService extends BaseService
{
    protected static $model='model\EmployeePerformanceModel';

    public static function addEmployeePerformance($hostipalId,$adminId,$treatmentIds,$moneys){

        $addtime = time();
        foreach($treatmentIds as $k=>$tid){
            $one = [
                'hospital_id'=>$hostipalId,
                'admin_id'=>$adminId,
                'treatment_id'=>$tid,
                'performance'=>$moneys[$k],
            ];
            $has = self::getInfo(['admin_id'=>$adminId,'treatment_id'=>$tid]);
            if($has){
                self::edit($has['id'],$one);
            }else{
                $one['addtime'] = $addtime;
                self::add($one);
            }
        }
        return true;
    }

    public static function getEmployeePerformance($adminId){

        $list = self::$model::getList(['hospital_id'=>self::getUserHospitalId(),'admin_id'=>$adminId]);
        $treatmentList = TreatmentService::getTreatmentList();

        if($list){
            $map = array_column($list,'performance','treatment_id');
            foreach($treatmentList as $key=>$value){
                foreach($value['list'] as $k=>$v){
                    if(isset($map[$v['id']])){
                        $v['performance'] = $map[$v['id']];
                    }
                    $value['list'][$k] = $v;
                }
                $treatmentList[$key] = $value;
            }
        }
        return $treatmentList;
    }
}